package xj.graph2d.app.prop;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class ColorGrid extends JPanel {

  static interface ColorChangeListener {
    public void colorChanged(Color c);
  }

  public ColorGrid(int cellWidth, int cellHeight, 
		   int xpad, int ypad, 
		   int row, int col) {
    this(cellWidth, cellHeight, xpad, ypad, new Color[row][col]);
  }

  public ColorGrid(int cellWidth, int cellHeight, 
		   int xpad, int ypad, 
		   Color[][] colorGrid) {
    if (cellWidth < 5) {
      cellWidth = 5;
    }
    if (cellHeight < 5) {
      cellHeight = 5;
    }
    if (xpad < 2) {
      xpad = 2;
    }
    if (ypad < 2) {
      ypad = 2;
    }
    this.cellWidth = cellWidth;
    this.cellHeight = cellHeight;
    this.xpad = xpad;
    this.ypad = ypad;
    this.colorGrid = colorGrid;
    rowCount = colorGrid.length;
    columnCount = colorGrid[0].length;
    dimension = new Dimension((cellWidth + xpad) * columnCount + xpad,
			      (cellHeight + ypad) * rowCount + ypad);
    numOfColors = 0;
    for (int i = 0; i < rowCount; i++) {
      for (int j = 0; j < columnCount; j++) {
	if (colorGrid[i][j] != null) {
	  numOfColors++;
	}
      }
    }

    addMouseListener(new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent event) {
	Point p = event.getPoint();
	int i = (p.y / (ColorGrid.this.cellHeight + ColorGrid.this.ypad));
	int j = (p.x / (ColorGrid.this.cellWidth + ColorGrid.this.xpad));
	if (i < rowCount && j < columnCount) {
	  setColor(i, j);
	  repaint();
	  if (listener != null) {
	    listener.colorChanged(color);
	  }
	}
      }
    });
  }

  public void addColorChangeListener(ColorChangeListener l) {
    listener = l;
  }

  public void setColor(Color color) {
    this.color = color;
  }

  public void setColor(int i, int j) {
    color = colorGrid[i][j];
  }

  public Color getColor(int i, int j) {
    return colorGrid[i][j];
  }

  public Color getColor() {
    return color;
  }

  @Override
  public Dimension getMinimumSize() {
    return dimension;
  }

  @Override
  public Dimension getPreferredSize() {
    return dimension;
  }

  @Override
  public void paint(Graphics g) {
    Dimension dim = getSize();
    g.setColor(Color.lightGray);
    g.fillRect(0, 0, dim.width, dim.height);
    int x, y;
    for (int i = 0; i < rowCount; i++) {
      for (int j = 0; j < columnCount; j++) {
	x = (cellWidth + xpad) * j + xpad;
	y = (cellHeight + ypad) * i + ypad;
	g.setColor(colorGrid[i][j]);
	g.fillRect(x, y, cellWidth, cellHeight);
	g.setColor(Color.black);
	g.drawRect(x, y, cellWidth, cellHeight);
      }
    }
  }

  protected Color color;

  protected int cellWidth;

  protected int cellHeight;

  protected int rowCount;

  protected int columnCount;

  protected int xpad;

  protected int ypad;

  protected Dimension dimension;

  protected Color[][] colorGrid = null;

  protected int numOfColors = 0;

  protected ColorChangeListener listener = null;

  public static Color defaultColor = Color.lightGray;

  public static Color[][] defaultColorGrid = {
      { Color.white, Color.lightGray, Color.darkGray, Color.black },
      { Color.gray, Color.blue, Color.cyan, Color.green },
      { Color.yellow, Color.orange, Color.pink, Color.red },
      { Color.magenta, new Color(230, 230, 250), 
	new Color(0, 0, 128), new Color(64, 224, 208) } };

}
